package com.wolves.career.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolves.career.entity.IndustryInformationClassification;
import com.wolves.career.entity.IndustryInformationInformation;
import com.wolves.career.entity.excel.InfoExcel;
import com.wolves.career.service.IndustryInformationClassificationService;
import com.wolves.career.service.IndustryInformationInformationService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.Random;

/**
 * @author 麦翔盛
 * @description: 咨询excel监听类
 * @date 2021/10/25 20:17
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InfoExcelListener extends AnalysisEventListener<InfoExcel> {
    private IndustryInformationClassificationService classificationService;
    private IndustryInformationInformationService informationService;

    @Override
    public void invoke(InfoExcel infoExcel, AnalysisContext analysisContext) {
        //获取分分类id
        String classification = infoExcel.getClassification();
        String classificationId = getClassificationByName(classificationService, classification);
        IndustryInformationInformation information = new IndustryInformationInformation();
        //将excel中的值赋给information中
        information.setContent(infoExcel.getContent());
        information.setTitle(infoExcel.getTitle());
        information.setClassificationId(classificationId);
        //设置阅读量
        information.setViews(new Random().nextInt(10000));
        information.setAuthor("张三");
        information.setReleaseStaffId("112233445566");
        informationService.save(information);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 根据分类名获取分类id
     * @param classificationService
     * @param name
     * @return
     */
    public String getClassificationByName
    (IndustryInformationClassificationService classificationService,String name){
        QueryWrapper<IndustryInformationClassification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classification",name);
        IndustryInformationClassification classification = classificationService.getOne(queryWrapper);
        if (classification!=null){
            return classification.getClassificationId();
        }
        return null;
    }
}
